<?php
// +----------------------------------------------------------------------
// | Citrix [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.tpCitrix.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.tpCitrix.com/ )
// +----------------------------------------------------------------------
// | Author: Citrix < 2722279500@qq.com >
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\controller\Permissions;
use \think\Db;
use \think\Cookie;
use \think\Session;
use \think\Cache;
use app\admin\model\Link as linkModel;

class Link extends Permissions
{
	//友情链接
	public function link()
	{
        $model = new linkModel();
        $link = empty($where) ? $model->order('show_order desc')->paginate(20) : $model->where($where)->order('show_order desc')->paginate(20,false,['query'=>$this->request->param()]);
        $this->assign('link',$link);
		return $this->fetch();
	}
	//友情链接 编辑（新增）
	public function link_publish()
	{
		//获取菜单id
    	$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    	$model = new linkModel();
		//是正常添加操作
		if($id > 0) {
    		//是修改操作
    		if($this->request->isPost()) {
    			//是提交操作
    			$post = $this->request->post();
	            //验证菜单是否存在
	            $link = $model->where('id',$id)->find();
	            if(empty($link)) {
	            	return $this->error('id不正确');
	            }
                //设置修改人
                $post['edit_admin_id'] = Session::get('admin');
	            if(false == $model->allowField(true)->save($post,['id'=>$id])) {
	            	return $this->error('修改失败');
	            } else {
                    addlog($model->id);//写入日志
	            	return $this->success('修改成功','admin/Link/link');
	            }
    		} else {
    			//非提交操作
    			$link = $model->where('id',$id)->find();
                if(!empty($link)) {
                    $this->assign('link',$link);
                    return $this->fetch();
                } else {
                    return $this->error('id不正确');
                }
    		}
    	} else {
    		//是新增操作
    		if($this->request->isPost()) {
    			//是提交操作
    			$post = $this->request->post();
	            if(false == $model->allowField(true)->save($post)) {
	            	return $this->error('添加失败');
	            } else {
                    addlog($model->id);//写入日志
	            	return $this->success('添加成功','admin/Link/link');
	            }
    		} else {
    			//非提交操作
    			return $this->fetch();
    		}
    	}
	}
	//删除友情链接
	public function link_delete()
    {
    	if($this->request->isAjax()) {
    		$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            if(false == Db::name('link')->where('id',$id)->delete()) {
                return $this->error('删除失败');
            } else {
                addlog($id);//写入日志
                return $this->success('删除成功','admin/Link/link');
            }
    	}
    }
    /**
    * 友情链接快捷编辑
    * ajax 修改指定表数据字段  一般修改状态 比如 是否推荐 是否开启 等 图标切换的
    * table,id_name,id_value,field,value
    */
    public function link_changeTableVal(){  
        $table = input('table'); // 表名
        $id_name = input('id_name'); // 表主键id名
        $id_value = input('id_value'); // 表主键id值
        $field  = input('field'); // 修改哪个字段
        $value  = input('value'); // 修改字段值  
        $addlog = addlog($id_value);
        if(!empty($addlog))
        {
            return $addlog;
        }else
        {
            Db::name($table)->where([$id_name=>$id_value])->update([$field=>$value]); // 根据条件保存修改的数据
            return $this->success('更新成功','admin/Link/link');
           //echo json_encode(array('msg'=>'更新成功'));exit;
        }
    }
	
}




?>